// 导入request模块
import request from './request'
import { isMockMode } from '../config';

/**
 * 管理员登录
 * @param {Object} params - 登录参数
 * @param {Object} params.user_info - 用户信息对象
 * @returns {Promise<Object>} 登录结果
 */
export default async function adminLogin(params = {}) {
  try {
    // 提取用户信息
    const { user_info = {} } = params;
    
    // 验证必填字段
    if (!user_info.username || !user_info.password) {
      throw new Error('管理员登录信息不完整：用户名和密码为必填项');
    }
    
    // 构造请求数据
    const requestData = {
      username: user_info.username,
      password: user_info.password,
      name: user_info.name || '',
      phone: user_info.phone || ''
    };
    
    console.log('管理员登录请求数据:', requestData);
    
    if (isMockMode) {
      // 模拟登录响应
      return {
        data: {
          token: 'mock_admin_token_' + Date.now(),
          user_info: {
            id: '3',
            username: user_info.username,
            name: user_info.name || '管理员',
            phone: user_info.phone || '',
            user_type: 'admin',
            role: 'admin'
          }
        }
      };
    }
    
    // 调用实际登录接口
    const response = await request.post('/auth/admin/login', requestData);
    return response;
  } catch (error) {
    console.error('管理员登录失败:', error);
    throw error;
  }
}

// 管理员相关的其他API接口
export const getUsersList = (page = 1, size = 10, role = '') => {
  return request.get('/admin/users', {
    page,
    size,
    role
  })
}

export const getUserDetails = (userId) => {
  return request.get(`/admin/users/${userId}`)
}

export const updateUserStatus = (userId, status) => {
  return request.put(`/admin/users/${userId}/status`, {
    status
  })
}

export const getAllAssessments = (page = 1, size = 10) => {
  return request.get('/admin/assessments', {
    page,
    size
  })
}